__author__ = 'xiaosu'


from flask import Flask
from flask_wtf.csrf import CsrfProtect
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
import string,random
from werkzeug.contrib.cache import  SimpleCache,MemcachedCache
import os
basedir = os.path.abspath(os.path.dirname(__file__))


#from flask_debugtoolbar import DebugToolbarExtension


csrf = CsrfProtect()


app=Flask(__name__)
app.config.from_object('config')
#app.debug = True
app.config['SQLALCHEMY_RECORD_QUERIES'] = True

csrf.init_app(app)
db = SQLAlchemy(app)
lm=LoginManager()
lm.init_app(app)
lm.login_view = 'login'
lm.session_protection = "strong"
#toolbar = DebugToolbarExtension(app)

cache = SimpleCache()
#cache=MemcachedCache(['127.0.0.1:11211'])

# print app.config.get("ADMIN_PASSWORD")

if not app.debug:
    import logging
    from logging.handlers import TimedRotatingFileHandler
    loggers = [app.logger, logging.getLogger('sqlalchemy'),logging.getLogger('octopus')]
    fh=TimedRotatingFileHandler(os.path.join(basedir,'./log/octopus.log'),"D",1,100)
    fh.suffix= "-%Y-%m-%d-%H-%M"
    fh.setFormatter(logging.Formatter('%(asctime)s    %(levelname)s   %(message)s  [in %(pathname)s:%(lineno)d]'))
    app.logger.setLevel(logging.INFO)
    fh.setLevel(logging.INFO)
    app.logger.addHandler(fh)
    # for logger in loggers:
    #     logger.addHandler(fh)


from octopus import views,models

